// id-manager.js

var IDManager = {};

IDManager.load = function() {
	AppalachianDB.init();
	IDManager.refresh();
};

IDManager.unload = function() {
	// do nothing
};

IDManager.refresh = function() {
	if (AppalachianDebug.enabled()) AppalachianDebug.trace("id-manager.js", "> refresh");
	var prefs = Components.classes["@mozilla.org/preferences-service;1"].getService(Components.interfaces.nsIPrefBranch);
	var defaultID = prefs.getIntPref("extensions.appalachian.defaultID");
	var defaults = prefs.getBoolPref("extensions.appalachian.useDefault");

	var ids = AppalachianDB.cosmeticListAll();
	var list = document.getElementById("existing-openids");
	for (var i = list.getRowCount(); i > 0; i--) {
		list.removeItemAt(i-1);
	}
	for (var i = 0; i < ids.length; i++) {
	 	var id = ids[i];
		var item = document.createElement("listitem");
		item.setAttribute("value", id.getID());

		var listcell;

		listcell = document.createElement("listcell");
		listcell.className = "normal-id";
		if (defaults && id.getID() == defaultID) {
			listcell.className = "default-id";
		}
		item.appendChild(listcell);

		listcell = document.createElement("listcell");
		listcell.setAttribute("label", id.getURL());
		// display without scheme; do not use, undefined for
		// OpenID 1.1
		// listcell.setAttribute("label", id.getDisplayURL());
		item.appendChild(listcell);

		listcell = document.createElement("listcell");
		listcell.setAttribute("label", id.getUse());
		item.appendChild(listcell);

		list.appendChild(item);
	}
	if (list.selectedCount == 0) {
		IDManager.unselect();
	}
	if (AppalachianDebug.enabled()) AppalachianDebug.trace("id-manager.js", "< refresh");
}

IDManager.selected = function() {
	var prefs = Components.classes["@mozilla.org/preferences-service;1"].getService(Components.interfaces.nsIPrefBranch);
	var defaults = prefs.getBoolPref("extensions.appalachian.useDefault");
	document.getElementById("id-manager-modify-button").disabled = false;
	document.getElementById("id-manager-delete-button").disabled = false;
	document.getElementById("id-manager-view-servers-button").disabled = false;
	document.getElementById("id-manager-view-associated-button").disabled = false;
	if (defaults && IDManager.current() && !IDManager.current().isDefault())
		document.getElementById("id-manager-default-button").disabled = false;
	else
		document.getElementById("id-manager-default-button").disabled = true;
}

IDManager.unselect = function() {
	document.getElementById("id-manager-modify-button").disabled = true;
	document.getElementById("id-manager-delete-button").disabled = true;
	document.getElementById("id-manager-default-button").disabled = true;
	document.getElementById("id-manager-view-servers-button").disabled = true;
	document.getElementById("id-manager-view-associated-button").disabled = true;
}

IDManager.current = function() {
	var cur = null;
	var list = document.getElementById("existing-openids");
	var item = list.selectedItem;
	if (item) {
		var atts = item.childNodes;
		var cur = new OpenID(item.value, atts[1].getAttribute("label"), atts[2].getAttribute("label"));
		if (atts[0].getAttribute("checked")) cur.setIsDefault(true);
	}
	return cur;
};

IDManager.add = function() {
	// open a dialog
	if (AppalachianDebug.enabled()) AppalachianDebug.trace("id-manager.js", "> add");
	window.openDialog("chrome://appalachian/content/add-openid.xul", "appalachian-add-openid", "chrome,modal");
	IDManager.refresh();
	if (AppalachianDebug.enabled()) AppalachianDebug.trace("id-manager.js", "< add");
};

IDManager.modify = function() {
	// open a dialog
	var row = IDManager.current();
	window.openDialog("chrome://appalachian/content/modify-openid.xul", "appalachian-modify-openid", "chrome,modal", { id : row.getID() } );
	IDManager.refresh();
};

IDManager.count = function() {
	var list = document.getElementById("existing-openids");
	return list.getRowCount();
}

IDManager.delete = function() {
	if (AppalachianDebug.enabled()) AppalachianDebug.trace("id-manager.js", "> delete");
	var row = IDManager.current();
	var messages = document.getElementById("id-manager-string-bundle");
	if (row.isDefault() && IDManager.count() > 1) {
		var deldefault = messages.getString("deletedefault");
		alert(deldefault);
		return;
	}
	var delconfirm = messages.getString("deleteconfirm");
	var doit = confirm(delconfirm + " '" + row.getURL() + "'?");
	// display without scheme; do not use, undefined for OpenID 1.1
	// var doit = confirm(delconfirm + " '" + row.getDisplayURL() + "'?");
	if (doit) {
		try {
			AppalachianDB.delete(row.getID());
			IDManager.refresh();
		} catch (e) {
			var delerr = messages.getString("deleteerror");
			alert(delerr + " " + e);
		}
	}
	if (AppalachianDebug.enabled()) AppalachianDebug.trace("id-manager.js", "< delete");
};

IDManager.setDefault = function() {
	var row = IDManager.current();
	var prefs = Components.classes["@mozilla.org/preferences-service;1"].getService(Components.interfaces.nsIPrefBranch);
	prefs.setIntPref("extensions.appalachian.defaultID", row.getID());
	IDManager.refresh();
};

IDManager.viewServers = function() {
	var row = IDManager.current();
	window.openDialog("chrome://appalachian/content/list-openid-servers.xul", "appalachian-list-openid-servers", "chrome,modal,width=500,height=350", { id : row.getID(), name : row.getUse() } );
};

IDManager.viewSites = function() {
	var row = IDManager.current();
	window.openDialog("chrome://appalachian/content/list-openid-uses.xul", "appalachian-list-openid-uses", "chrome,modal,width=500,height=350", { id : row.getID(), name : row.getUse() } );
};

IDManager.close = function() {
	window.close();
};
